package com.xn.myyxcampus.fragment;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ImageView;
import android.widget.ListView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;
import com.xn.myyxcampus.R;
import com.xn.myyxcampus.adapter.VoiceListAdatper;
import com.xn.myyxcampus.bean.NewsList;
import com.xn.myyxcampus.bean.VoiceList;
import com.xn.myyxcampus.http.URLHelper;
import com.xn.myyxcampus.http.URLs;
import com.xn.myyxcampus.utils.JacksonUtils;
import com.xn.myyxcampus.utils.ToastUtils;
import com.xn.myyxcampus.view.PullDownView;
import com.xn.myyxcampus.view.PullDownView.OnPullDownListener;

public class VoiceFragment extends BaseFragment implements OnClickListener,
		OnPullDownListener {

	private PullDownView pull_down_view;
	private ListView mListView;
	private ImageView showMenu;
	private ImageView publishVoice;
	private ImageView voiceLoading;
	private VoiceListAdatper voiceListAdapter = new VoiceListAdatper();
	private VoiceList voiceList;

	private static final int WHAT_DID_LOAD_DATA = 0;
	/** Handler What更新数据完毕 **/
	private static final int WHAT_DID_REFRESH = 1;
	/** Handler What更多数据完毕 **/
	private static final int WHAT_DID_MORE = 2;

	int currentPage = 2;

	@Override
	public View initView(LayoutInflater inflater) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.voice, null);
		pull_down_view = (PullDownView) view.findViewById(R.id.voicList);
		showMenu = (ImageView) view.findViewById(R.id.ivvoic_showMenu);
		publishVoice = (ImageView) view.findViewById(R.id.iv_publishVoice);
		voiceLoading = (ImageView) view.findViewById(R.id.iv_voiceLoading);
		mListView = pull_down_view.getListView();
		mListView.setAdapter(voiceListAdapter);
		mListView.setVisibility(View.GONE);

		animationDrawable = (AnimationDrawable) voiceLoading.getBackground();
		animationDrawable.start();

		publishVoice.setOnClickListener(this);
		showMenu.setOnClickListener(this);
		pull_down_view.setOnPullDownListener(this);
		return view;
	}

	private HttpUtils httpUtil = new HttpUtils();

	@Override
	public void initData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		httpUtil.send(HttpRequest.HttpMethod.GET,
				URLHelper.getURL(URLs.VOICE_LIST_LOAD, null),
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						ToastUtils.showToast("网络异常~~淡定~~~");
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						loadData(arg0.result, WHAT_DID_LOAD_DATA);
					}
				});
	}

	private void loadData(String result, int action) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			HashMap<String, Object> map = (HashMap<String, Object>) mapper
					.readValue(result, HashMap.class);
			int response = (Integer) map.get("response");
			LogUtils.i("response:" + response);
			switch (action) {
			case WHAT_DID_LOAD_DATA:
				voiceList = JacksonUtils.jsonToBean(result, VoiceList.class);
				mListView.setVisibility(View.VISIBLE);
				Animation animation = AnimationUtils.loadAnimation(ct,
						R.anim.list_anim);
				LayoutAnimationController controller = new LayoutAnimationController(
						animation, 10);
				mListView.setLayoutAnimation(controller);
				animationDrawable.stop();
				voiceLoading.setVisibility(View.GONE);
				currentPage = 2;
				break;
			case WHAT_DID_MORE:
				if (1 == response) {
					VoiceList moreVoice = JacksonUtils.jsonToBean(result,
							VoiceList.class);
					if (moreVoice.getList().size() > 0) {
						voiceList.getList().addAll(moreVoice.getList());
					}
				} else {
					ToastUtils.showToast("没有更多数据了哦！~~~");
				}
				currentPage++;
				break;
			case WHAT_DID_REFRESH:
				voiceList.getList().clear();
				voiceList = JacksonUtils.jsonToBean(result, VoiceList.class);
				currentPage = 2;
				break;
			}
			voiceListAdapter.loadData(voiceList);
			voiceListAdapter.notifyDataSetChanged();
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.ivvoic_showMenu:
			showLeft();
			break;
		case R.id.iv_publishVoice:
			break;
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Thread.sleep(2000);
					pull_down_view.RefreshComplete();
					httpUtil.send(HttpRequest.HttpMethod.GET,
							URLHelper.getURL(URLs.VOICE_LIST_LOAD, null),
							new RequestCallBack<String>() {
								@Override
								public void onFailure(HttpException arg0,
										String arg1) {
									// TODO Auto-generated method stub
									ToastUtils.showToast("网络访问异常..淡定~~");
								}

								@Override
								public void onSuccess(ResponseInfo<String> arg0) {
									// TODO Auto-generated method stub
									LogUtils.i("statusCode" + arg0.statusCode);
									LogUtils.i("result" + arg0.result);
									loadData(arg0.result, WHAT_DID_REFRESH);
								}
							});
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	@Override
	public void onMore() {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				pull_down_view.notifyDidMore();
				NameValuePair page_pair = new BasicNameValuePair("page",
						currentPage + "");
				httpUtil.send(HttpRequest.HttpMethod.GET,
						URLHelper.getURL(URLs.VOICE_LIST_MORE, page_pair),
						new RequestCallBack<String>() {
							@Override
							public void onFailure(HttpException arg0,
									String arg1) {
								// TODO Auto-generated method stub
								ToastUtils.showToast("网络访问异常..淡定~~");
							}

							@Override
							public void onSuccess(ResponseInfo<String> arg0) {
								// TODO Auto-generated method stub
								LogUtils.i("statusCode" + arg0.statusCode);
								loadData(arg0.result, WHAT_DID_MORE);
								LogUtils.i(currentPage + "" + arg0.result);
							}
						});
			}
		}).start();
	}

}
